// pages/functions/articledetail.js
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    functype: '',
    openid: '',
    seqid: '',
    title: '',
    indexing: '',
    author: '',
    activity: '',
    imageurl:'',
    updatetime: '页面加载中，请稍后······',
    articleDetails: [],
    userinfo:[],
    loading: false,
    disabled: false,
    collected:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    var openid = self.options.openid;
    var seqid = self.options.seqid;

    self.setData({
      functype: self.options.functype,
      openid: self.options.openid,
      seqid: self.options.seqid
    })
  },
//图片加载完再查内容
queryDetail(){
  var self = this;
  
  qcloud.request({
    url: `${config.service.host}/weapp/articleManage`,
    method: 'POST',
    data: { opflag: '1', openid: self.data.openid, seqid: self.data.seqid }, //opflag 0获取列表 1查看明细 2新增 3更新 4删除
    login: true,
    success(result) {
      if (result.data.data.status == '0') {
        var publishdate = util.formatDate(result.data.data.msg.updatetime);

        var articleDetails = result.data.data.msg.detaildata;
        for (var i = 0; i < articleDetails.length; i++) {
          articleDetails[i].style = JSON.parse(articleDetails[i].style);
        }
        //console.log(result.data.data.msg.userinfo)
        self.setData({
          activity: result.data.data.msg.activity,
          title: result.data.data.msg.title,
          indexing: result.data.data.msg.indexing,
          author: result.data.data.msg.author,
          userinfo: result.data.data.userinfo,
          imageurl: result.data.data.msg.imageurl,
          updatetime: publishdate,
          articleDetails: articleDetails,
        })
        if (self.data.openid != result.data.data.userinfo){
          self.whetherCollected();
        }
      } else {
        console.log('add acticle error:', result.data.data);
        self.setData({
          updatetime: result.data.data.msg
        })
      }
    },
    fail(error) {
      console.log('request fail', error);
      util.relogin(self);
    }
  })
  // var setUserInfo = function (results){
  //   self.setData({
  //     userinfo: results
  //   })
  // }
  // util.relogin(self, setUserInfo)
  
},
//查询是否收藏
whetherCollected:function(){
  var self = this
  //查询收藏情况
  qcloud.request({
    url: `${config.service.host}/weapp/collectingManage`,
    method: 'POST',
    data: { opflag: '3', author: self.data.openid, seqid: self.data.seqid }, //opflag 0查看收藏列表 1新增收藏 2取消收藏 3根据内容判断当前用户是否收藏
    login: true,
    success(result) {
      if (result.data.data.status == '0') {
        self.setData({
          collected: result.data.data.msg,
        })
      } else {
        console.log('add acticle error:', result.data.data);
      }
    },
    fail(error) {
      console.log('request fail', error);
      util.relogin(self);
    }
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.queryDetail();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.queryDetail();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var self = this
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: self.data.title,
      path: '/pages/functions/articledetail?functype=' + self.data.functype + '&openid=' + self.data.openid + '&seqid=' + self.data.seqid,
      imageUrl: self.data.imageurl,
      success: function (res) {
        // 转发成功
        //util.showSuccess('转发成功')
      },
      fail: function (res) {
        // 转发失败
        console.log('转发失败',res)
      }
    }
  },

  imageloaderr: function (e) {
    var self = this;
    var tmpArticleDetail = self.data.articleDetails;
    tmpArticleDetail[e.currentTarget.dataset.orders].elmtvalue = "";
    self.setData({
      articleDetails: tmpArticleDetail
    })
  },
  modifyArticle:function(){
    var self = this;
    wx.redirectTo({
      url: '/pages/functions/articleupdate?functype=' + self.data.functype + '&seqid=' + self.data.seqid + '&openid=' + self.data.openid 
    })
  },
  delArticle: function () {
    var self=this;
    self.setData({
      loading: !self.data.loading,
      disabled: !self.data.disabled
    })
    qcloud.request({
      url: `${config.service.host}/weapp/articleManage`,
      method: 'POST',
      data: { opflag: '4', seqid: self.data.seqid }, //opflag 0获取列表 1查看明细 2新增 3更新 4删除
      login: true,
      success(result) {
        if (result.data.data.status == '0') {
          wx.navigateBack({
                  delta: 1
                })          
        } else {
          console.log('delete acticle error:', result.data.data);
          self.setData({
            loading: !self.data.loading,
            disabled: !self.data.disabled
          })
        }
      },
      fail(error) {
        console.log('request fail', error);
        util.relogin(self);
      }
    })
  },
  bindCollecting:function(){
    var self=this;
    if(self.data.disabled){
      return
    }

    self.setData({
      disabled: !self.data.disabled,
    })

    var queryflag='1';//1收藏 2取消
    if(self.data.collected){
      queryflag='2';
    }
    qcloud.request({
      url: `${config.service.host}/weapp/collectingManage`,
      method: 'POST',
      data: { opflag: queryflag, author: self.data.openid, seqid: self.data.seqid, functype: self.data.functype }, //opflag 0查看收藏列表 1新增收藏 2取消收藏 3根据内容判断当前用户是否收藏
      login: true,
      success(result) {
        if (result.data.data.status == '0') {
          self.setData({
            collected: !self.data.collected,
            disabled: !self.data.disabled,
          })
          util.showSuccess(result.data.data.msg)
        } else {
          console.log('add acticle error:', result.data.data);
          self.setData({
            disabled: !self.data.disabled,
          })
        }
        
      },
      fail(error) {
        console.log('request fail', error);
        util.relogin(self);
        self.setData({
          disabled: !self.data.disabled,
        })
      }
    })
  }
})